import React, {useEffect} from 'react';
import {useNavigation, useRoute} from '@react-navigation/native';
import store from '../store';
const getPeriodTime = () => {
  const now = Date.now();
  const periodTime = {};
  // const timing = window.performance.timing;
  // periodTime.redirect = timing.redirectEnd - timing.redirectStart; // url重定向消耗时间
  // periodTime.cache = timing.domainLookupStart - timing.fetchStart; // 读取缓存消耗时间
  // periodTime.dns = timing.domainLookupEnd - timing.domainLookupStart; // dns解析消耗时间
  // periodTime.tcp = timing.connectEnd - timing.connectStart; // 建立TCP链接消耗时间
  // periodTime.request = timing.responseStart - timing.requestStart; // 发起请求消耗时间
  // periodTime.response = timing.responseEnd - timing.responseStart; // 响应请求消耗时间
  // periodTime.processing = timing.loadEventStart - timing.domLoading; // js逻辑处理消耗时间
  // periodTime.onload = now - timing.loadEventStart; // 页面加载结束总消耗时间（ms毫秒）
  return periodTime;
};

class TrackData {
  constructor() {
    this.store = null;
    this.trackBaseData = {};
    this.trackMergeData = [];
    this.trackMergeExposureData = [];
    this.beforeActivated = true;
  }

  init(stroe) {
    this.store = store.getState();
  }

  /**
   * 添加当前页面点击事件数据
   * @param {*} clickName 点击事件名称, 注意命名全小写加下划线 aa_bb_cc
   * @param {*} otherObj 点击事件其他额外附加参数
   */
  appendClickTrackData(clickName, otherObj = null) {
    console.log('appendClickTrackData');
  }
  /**
   * 添加当前页面显示区域跟踪数据
   * @param {*} showName 显示区域名称, 注意命名全小写加下划线 aa_bb_cc
   * @param {*} otherObj 其他额外附加参数
   */
  appendShowTrackData(el, otherObj = null) {
    console.log('appendShowTrackData');
  }
  /**
   * 添加当前页面浏览跟踪数据
   * @param {*} pageId 页面ID
   * @param {*} otherObj 其他额外附加参数
   */
  appendPageTrackData(pageId, otherObj = {}) {
    console.log('appendPageTrackData', this.store);
  }
  /**
   * 初始化基础参数
   */
  initBaseData() {
    // this.trackBaseData.url = this.getABTestParamsUrl
    // this.trackBaseData.last_url = this.lastUrl
    // this.trackBaseData.session_id = getSessionStorage('track_session_id')
    // this.trackBaseData.user_id = this.userId
    // this.trackBaseData.landing_page = getLocalStore('landing_page') || getCookie('landing_page')
    // this.trackBaseData.adlk_id = getCookie('adlk_id') || ''
    // this.trackBaseData.new_adlink_id = getCookie('new_adlk_id') || ''
    // delRootDomainCookie('new_adlk_id')
    // this.trackBaseData.position = getSessionStorage('last_click_position') || ''
    // this.trackBaseData.home_position = getSessionStorage('home_position') || ''
    // this.trackBaseData.source = getCookie('g_SOURCE') || ''
    // this.trackBaseData.patpat_experiments = this.getABTestExperiments(false)
    // this.trackBaseData.experiments = this.getABTestExperiments()
    // this.trackBaseData.abb = this.$i18n.locale || 'en'
    // this.trackBaseData.device = device()
    // const deviceLanguage = navigator.languages && navigator.languages.length > 0 ? navigator.languages[0] : ''
    // this.trackBaseData.device_language = deviceLanguage || navigator.language || navigator.browserLanguage || navigator.userLanguage || ''
    // this.trackBaseData.unique_session_id = getCookie('unique_session_id')
    // this.trackBaseData.patpat_platform = 'wap'
    // this.trackBaseData.is_automated = navigator.webdriver ? 1 : 0
    // if (this.$route.query.platform && this.$route.query.platform === 'app') {
    //   this.trackBaseData = Object.assign(this.trackBaseData, appTrackData())
    // }
  }
  /**
   * 上报合并过的流量点击显示数据
   * @param {Boolean} sendBeacon 是否使用sendBeacon方式上报
   */
  postTackData(sendBeacon = false) {
    console.log('postTackData');
  }
  /**
   * 上报合并过的坑位曝光数据
   * @param {Boolean} sendBeacon 是否使用sendBeacon方式上报
   */
  postExposureTrack(sendBeacon = false) {
    console.log('postExposureTrack');
  }
  /**
   * 处理曝光数据
   * @param {*} el
   */
  appendExposureData() {}
}
export const trackData = new TrackData();
export const useTrackData = () => {
  const navigation = useNavigation();
  const route = useRoute();
  useEffect(() => {
    console.log('focus tabPress blur', route);
    // focus tabPress blur
    const unfocus = navigation.addListener('focus', () => {
      trackData.appendPageTrackData();
      console.log('进来:....', route.name);
    });
    const unblur = navigation.addListener('blur', () => {
      // 页面失焦， 离开当前页面
      console.log('离开:....', route.name);
      trackData.appendClickTrackData('close_single_page', {
        period_time: JSON.stringify(getPeriodTime()),
      });
      trackData.postTackData(true);
      trackData.postExposureTrack(true);
    });
    const unbeforeRemove = navigation.addListener('beforeRemove', () => {
      console.log('beforeRemove');
    });
    return () => {
      unbeforeRemove();
      unfocus();
      unblur();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
};

export default {
  useTrackData,
  trackData,
};
